package au.com.betaversion.aspect;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import au.com.betaversion.domain.AircraftApiLog;
import au.com.betaversion.repository.AircraftApiLogRepository;
import au.com.betaversion.service.IpService;

@Aspect
@Component
public class ApiAspect {
	
	@Autowired
	private IpService ipService;
	
	@Autowired
	private AircraftApiLogRepository aircraftApiLogRepository;
	
	@After("execution(* au.com.betaversion.ApiController.getAircraftDetailes(..)) && " +
			"args(aircraftId, request)")
	public void logApiCall(String aircraftId, HttpServletRequest request) {
		AircraftApiLog aircraftApiLog = new AircraftApiLog(new Date(), ipService.getIp(request), aircraftId);
		aircraftApiLogRepository.save(aircraftApiLog);
	}

}
